﻿using System;
using System.Text;
using System.Configuration;
using System.Windows.Forms;
using System.IO;

namespace DoeCSLogin
{
    public class DoeCSLogin
    {
        public string _ssoUrl = "http://localhost:9090/";
        public string _serviceUrl = "http://localhost:9090";
        public string _cookieName = "ProviderSession";
        public int _CookieTimeOut = 30;
        /// <summary>
        /// 初始化
        /// </summary>
        public DoeCSLogin()
        {
            if (System.IO.File.Exists(Application.StartupPath.ToString() + @"\DoeCsLogin.exe"))
            {
                _ssoUrl = ConfigurationSettings.AppSettings["SSOUrl"];
                _ssoUrl = (char.Equals(_ssoUrl[_ssoUrl.Length - 1], '/')) ? _ssoUrl : _ssoUrl + "/";
                _serviceUrl = ConfigurationSettings.AppSettings["WebServiceUrl"];
                _serviceUrl = (char.Equals(_serviceUrl[_serviceUrl.Length - 1], '/')) ? _serviceUrl : _serviceUrl + "/";
                _cookieName = ConfigurationSettings.AppSettings["CookieName"];
                _CookieTimeOut = Int32.Parse(ConfigurationSettings.AppSettings["CookieTimeOut"]);
            }

        }

        /// <summary>
        /// 检测登录，如果系统登录过，返回当前登录人的账号，例如：JI-胡威
        /// </summary>
        /// <returns>用户</returns>
        public string Doe_Login()
        {
            string userID = "";

            int size = 1000;
            StringBuilder cookie = new StringBuilder(size);

            if (!InetCookie.InternetGetCookie(_ssoUrl, _cookieName, cookie, ref size))
            {
                return "";
            }
            string[] args = new string[1];
            args[0] = cookie.ToString().Replace(string.Format("{0}=", _cookieName), "");
            userID = WebServiceHelper.InvokeWebService(_serviceUrl + "DOELoginService.asmx", "GetLoginUser", args).ToString();
            return userID;
        }
        /// <summary>
        /// 登录用户
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>是否登录成功</returns>
        public bool Login(string username, string password)
        {
            bool result = false;

            string[] args = new string[2];
            args[0] = username;
            args[1] = password;

            //EnterpriseServerBase.WebService.DynamicWebCalling.DoeCookie
            string[] returnValue = (string[])WebServiceHelper.InvokeWebService(_serviceUrl + "DOELoginService.asmx", "CheckLogin", args);

            //DoeCookie myCookie = (DoeCookie)returnValue;
            if (!string.IsNullOrEmpty(returnValue[0]))
            {
                InetCookie.InternetSetCookie(_ssoUrl, _cookieName, string.Format("{0};expires={1}", returnValue[0], returnValue[1]));//+ ";expires=Sun,22-Feb-2099 00:00:00 GMT"
                result = true;
            }


            return result;
        }
        /// <summary>
        /// 登出本机用户
        /// </summary>
        /// <returns>是否成功</returns>
        public bool Logout()
        {
            bool result = false;

            string[] args = new string[1];
            args[0] = Doe_Login();

            string[] returnValue = (string[])WebServiceHelper.InvokeWebService(_serviceUrl + "DOELoginService.asmx", "LogoutUser", args);

            if (!string.IsNullOrEmpty(returnValue[0]))
            {
                if (!string.IsNullOrEmpty(returnValue[0]))
                {
                    InetCookie.InternetSetCookie(_ssoUrl, _cookieName, string.Format("{0};expires={1}", returnValue[0], returnValue[1]));//+ ";expires=Sun,22-Feb-2099 00:00:00 GMT"
                    result = true;
                }
            }
            return result;
        }

    }
}
